package com.rslai.commons.validator.jsonvalidator.validators;

import org.junit.Assert;

import java.util.Collection;
import java.util.List;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.Is.is;

/**
 * JSON校验器，非Empty校验器
 */
public class NoEmptyValidator extends Validator {
	public static final String VALIDATOR_NAME = "notempty";

    /**
     * 构造函数
     * @param args args
     */
    public NoEmptyValidator(List<Object> args) {

    }

    /**
     * 校验JSON对象
     * @param object 待验证的JSON对象
     */
	@Override
	public void doValidate(Object object) {
        Assert.assertThat(getCurrentKeyPath(), object, notNullValue());
        if (object instanceof List) {
            Assert.assertThat(getCurrentKeyPath(), (Collection<?>) object, is(not(empty())));
        } else {
            Assert.assertThat(getCurrentKeyPath(), getString(object), not(isEmptyString()));
        }
    }

    /**
     * 返回校验名称
     * @return 校验名称
     */
    @Override
	public String name() {
        return VALIDATOR_NAME;
	}
}
